package com.qqtech.qquan.user.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.qqtech.qquan.user.model.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.constant.CoreConst;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.PinyingUtil;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.activity.service.ActivitySignService;
import com.qqtech.qquan.circle.enums.CircleAuditStatusEnum;
import com.qqtech.qquan.circle.enums.CircleBaseStatusDictEnum;
import com.qqtech.qquan.circle.enums.CircleFuncTypeDictEnum;
import com.qqtech.qquan.circle.enums.CircleOpenLevelDictEnum;
import com.qqtech.qquan.circle.model.CircleBaseQuery;
import com.qqtech.qquan.circle.model.CircleBaseVo;
import com.qqtech.qquan.circle.model.CircleMemberQuery;
import com.qqtech.qquan.circle.model.CircleTopicQuery;
import com.qqtech.qquan.circle.model.CircleTopicVo;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.circle.service.CircleMemberService;
import com.qqtech.qquan.circle.service.CircleTopicService;
import com.qqtech.qquan.community.enums.CommunityJoinStatusDictEnum;
import com.qqtech.qquan.community.model.CommunityBaseQuery;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.model.CommunityJoinQuery;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.community.service.CommunityJoinService;
import com.qqtech.qquan.delivery.enums.DeliveryOrderStatusDictEnum;
import com.qqtech.qquan.delivery.model.DeliveryOrderQuery;
import com.qqtech.qquan.delivery.service.DeliveryOrderService;
import com.qqtech.qquan.hi.service.HiUserService;
import com.qqtech.qquan.homemaking.service.HomemakingBaseService;
import com.qqtech.qquan.mall.model.MallSupplierUser;
import com.qqtech.qquan.mall.service.MallSupplierUserService;
import com.qqtech.qquan.order.enums.OrderBaseDeliveryStatusDictEnum;
import com.qqtech.qquan.order.enums.OrderBaseOrderStatusDictEnum;
import com.qqtech.qquan.order.enums.OrderBasePayStatusDictEnum;
import com.qqtech.qquan.order.model.OrderBaseQuery;
import com.qqtech.qquan.order.service.OrderBaseService;
import com.qqtech.qquan.pub.service.PubCommentService;
import com.qqtech.qquan.pub.service.PubConcernService;
import com.qqtech.qquan.pub.service.PubPraiseService;
import com.qqtech.qquan.send.enums.SendDeliveryUserAuditStatusDictEnum;
import com.qqtech.qquan.send.model.SendDeliveryUser;
import com.qqtech.qquan.send.service.SendDeliveryUserService;
import com.qqtech.qquan.sys.enums.SysConst;
import com.qqtech.qquan.sys.model.SysParamVo;
import com.qqtech.qquan.sys.service.SysParamService;
import com.qqtech.qquan.user.enums.UserConst;
import com.qqtech.qquan.user.enums.UserIdentityTypeDictEnum;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserBlackService;
import com.qqtech.qquan.user.service.UserFriendApplyService;
import com.qqtech.qquan.user.service.UserFriendService;
import com.qqtech.qquan.user.service.UserIdentityService;
import com.qqtech.qquan.user.service.UserLoginService;
import com.qqtech.qquan.user.service.UserMessageService;
import com.qqtech.qquan.user.service.UserMoneyLogService;
import com.qqtech.qquan.user.service.UserValueService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/user/base")
public class UserBaseController extends BaseWebController<UserBase, UserBaseQuery> {
	@Resource
	private UserBaseService userBaseService;
	@Resource
	private UserLoginService userLoginService;
	@Resource
	private UserBlackService userBlackService;
	@Resource
	private CircleBaseService circleBaseService;
	@Resource
	private CircleMemberService circleMemberService;
	@Resource
	private CircleTopicService circleTopicService;
	@Resource
	private PubCommentService pubCommentService;
	@Resource
	private PubPraiseService pubPraiseService;
	@Resource
	private UserFriendApplyService userFriendApplyService;
	@Resource
	private UserFriendService userFriendService;
	@Resource
	private CommunityBaseService communityBaseService;

	@Resource
	private CommunityJoinService communityJoinService;

	@Resource
	private HomemakingBaseService homemakingBaseService;

	@Resource
	private PubConcernService pubConcernService;

	@Resource
	private UserMessageService userMessageService;

	@Resource
	private UserIdentityService userIdentityService;

	@Resource
	private UserValueService userValueService;

	@Resource
	private OrderBaseService orderBaseService;

	@Resource
	private DeliveryOrderService deliveryOrderService;

	@Resource
	private MallSupplierUserService mallSupplierUserService;

	@Resource
	private SendDeliveryUserService sendDeliveryUserService;

	@Resource
	private SysParamService sysParamService;

	@Resource
	private UserMoneyLogService userMoneyLogService;
	@Resource
	private HiUserService hiUserService;
	@Resource
	private ActivitySignService activitySignService;

	@Override
	protected BaseService<UserBase> getBaseService() {
		return userBaseService;
	}

	@RequestMapping(value = "/getLogo")
	@ResponseBody
	public Result getLogo(@RequestBody Map<String, String> map) {
		Result result = new Result();
		UserLoginVo userLoginVo = super.getLoginUser();
		boolean hasLogo = false;
		if (userLoginVo != null) {
			UserBaseVo userBaseVo = userBaseService.getById(userLoginVo.getId());
			if (StringUtil.isNotBlank(userBaseVo.getLogo())) {
				result.put("userLogo", userBaseVo.getFileDomain() + userBaseVo.getLogo());
				hasLogo = true;
			}
			result.put("hasIdentityAuth", userLoginVo.getHasIdentityAuth());
			result.put("hasCommunityAuth", userLoginVo.getHasCommunityAuth());
		}
		if (hasLogo == false) {
			result.put("userLogo", CoreConst.PARAMVALUE_CORE_FILE_DOMAIN + UserConst.DEFAULTVALUE_USER_LOGO);
		}
		return result;
	}

	/**
	 * 我-获取个人信息(userId、头像、昵称、手机账号信息)
	 * 
	 * @param map
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/get")
	@ResponseBody
	public Result get(@RequestBody Map<String, String> map) {
		UserLoginVo userLoginVo = super.getLoginUser();
		Result result = new Result();
		UserValueVo userValueVo = userValueService.getById(userLoginVo.getId());
		UserBaseVo userBaseVo = userBaseService.getById(userLoginVo.getId());
		double totalMoney = userMoneyLogService.getValidMoney(userLoginVo.getId());
		userValueVo.setMoney(new Double(totalMoney * CoreConst.VALUE_CORE_MONEYRATE).intValue());
		result.put("userValueVo", userValueVo);
		result.put("loginUser", userLoginVo);
		result.put("userBaseVo", userBaseVo);

		// 获取用户对应的供应商
		MallSupplierUser query = new MallSupplierUser();
		query.setUserId(userBaseVo.getId());
		List<MallSupplierUser> tempList = mallSupplierUserService.queryList(query);
		if (tempList != null && tempList.size() > 0) {
			result.put("supplierId", tempList.get(0).getSupplierId());
			OrderBaseQuery orderBaseQuery = new OrderBaseQuery();
			orderBaseQuery.setSupplierId(tempList.get(0).getSupplierId());
			orderBaseQuery.setOnlyToday(1);
			int todayOrderCount = orderBaseService.queryCountBySupplier(orderBaseQuery);
			result.put("todayCount", todayOrderCount);
		}
		SendDeliveryUser sendDeliveryUser = sendDeliveryUserService.getByUserId(userBaseVo.getId());
		if (sendDeliveryUser != null && sendDeliveryUser.getAuditStatus()
				.intValue() == SendDeliveryUserAuditStatusDictEnum.THROUGH.getCode()) {
			result.put("isSendDeliveryUser", 1);
			OrderBaseQuery orderBaseQuery = new OrderBaseQuery();
			orderBaseQuery.setHasSuperSendDelivery(YesNoEnum.NO.getCode());
			orderBaseQuery.setSendDeliveryUserId(super.getUserId());
			orderBaseQuery.setHasSendDelivery(2);
			int count = orderBaseService.queryCountBySendDeliveryUser(orderBaseQuery);
			result.put("unDeliveryCount", count);
		} else {
			result.put("isSendDeliveryUser", 0);
		}
		SysParamVo sysParamVo = sysParamService.getByKey(SysConst.PARAM_SYS_CUSTOMER_PHONE);
		if (null != sysParamVo) {
			result.put("customerPhone", sysParamVo.getValue());
		}
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/doUpdate")
	@ResponseBody
	public Result doUpdate(@RequestBody UserBase userBase) {
		Result ret = new Result();
		UserLoginVo userLoginVo = super.getLoginUser();
		if (userBase == null) {
			ret.addErr("参数不能为空");
		}
		if (userBase.getId() == null) {
			ret.addErr("参数不能为空");
		}
		if (!userLoginVo.getId().equals(userBase.getId())) {
			ret.addErr("无修改权限");
		}
		if (StringUtil.isNotBlank(userBase.getNick())) {
			String pinYin4Nick = PinyingUtil.hanziToPinyinNoSpace(userBase.getNick()).toUpperCase();
			if (StringUtil.isNotBlank(pinYin4Nick)) {
				userBase.setChnSpell(pinYin4Nick);// 全拼大写
			}
		}
		if (ret.checkForm()) {
			userBase.setUpdAt(TimeUtil.now());
			int v = userBaseService.updateById(userBase);
			ret.put("fullLogo", userBase.getFileDomain() + userBase.getLogo());
			userLoginService.clear(userLoginVo.getToken());
			if (v > 0) {
				if (StringUtil.isNotBlank(userBase.getNick())) {// 修改昵称得荣誉值
					hiUserService.editNick(userBase.getId());
				}
				if (StringUtil.isNotBlank(userBase.getLogo())) {// 修改头像得荣誉值
					hiUserService.uploadLogo(userBase.getId());
				}
			}
		}
		return ret;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Result detail(@RequestBody Map<String, String> map) {
		Result ret = new Result();
		UserLoginVo userLoginVo = super.getLoginUser();
		UserBaseVo userBaseVo = userBaseService.getById(userLoginVo.getId());
		ret.put("userBase", userBaseVo);
		ret.put("phone", userLoginVo.getPhone());
		return ret;
	}

	/**
	 * 访问其他用户的空间资料
	 * 
	 * @param userBaseQuery
	 * @return
	 */
	@RequestMapping(value = "/personal")
	@ResponseBody
	public Result personal(@RequestBody UserBaseQuery userBaseQuery) {
		Result ret = new Result();
		Integer curUserId = super.getUserId();
		UserBaseVo otherUserBaseVo = userBaseService.getById(userBaseQuery.getId());
		if (otherUserBaseVo == null) {
			ret.addErr("找不到这个用户!");
			return ret;
		}
		ret.put("otherUserBaseVo", otherUserBaseVo);
		boolean isMySelf = false;
		boolean isBlack = false;
		boolean isFriend = false;
		if (curUserId == userBaseQuery.getId()) {// 访问自己
			isMySelf = true;
		}
		isBlack = userBlackService.isBlack(curUserId, userBaseQuery.getId());// 是否黑名单
		isFriend = userFriendService.isFriend(curUserId, userBaseQuery.getId());// 是否是好友
		if (isFriend) {
			String friendRemark = "";
			UserFriendVo friend = userFriendService.queryNormalFriend(curUserId, userBaseQuery.getId());
			if (curUserId.intValue() == friend.getUserId1().intValue()) {
				friendRemark = friend.getRemark1();
			} else {
				friendRemark = friend.getRemark2();
			}
			ret.put("friendRemark", friendRemark);
		}
		ret.put("isBlack", isBlack);
		ret.put("isMySelf", isMySelf);
		ret.put("isFriend", isFriend);

		// 用户的动态
		CircleTopicQuery circleTopicQuery = new CircleTopicQuery();
		circleTopicQuery.setIsShow(YesNoEnum.YES.getCode());
		circleTopicQuery.setCreatorId(userBaseQuery.getId());// 版主

		Pageable pageKit2 = new PageKit(circleTopicQuery.getPage(), 1, new PageOrder(Direction.DESC, "newAt"));
		Page<CircleTopicVo> circleTopicPage = circleTopicService.queryPageList(circleTopicQuery, pageKit2);
		UserBaseVo userVo = null;
		for (CircleTopicVo topicVo : circleTopicPage) {
			userVo = userBaseService.getById(topicVo.getCreatorId());
			topicVo.setCreatorImg(userVo.getLogo());
			topicVo.setCreatorName(userVo.getNick());
			topicVo.setImgString(topicVo.getImg(), SysConst.IMG_POSTFIX);
		}
		ret.put("circleTopicPage", circleTopicPage);// 他、她的动态
		// 注册时间
		String registerTime = TimeUtil.getStringTimeByFormat(otherUserBaseVo.getNewAt(), TimeUtil.DATE_FORMAT);
		ret.put("registerTime", registerTime);// 注册时间
		// 他、她加入的圈子
		List<Integer> herCircleIds = circleMemberService.queryMyOpenCircleIds(userBaseQuery.getId());
		List<CircleBaseVo> circleList = circleBaseService.queryListByCircleIds(herCircleIds);
		ret.put("circleList", circleList);// 他、她加入的圈子

		// 他、她加入的社区
		List<Integer> communityIds = communityJoinService.queryMyCommuntiyIds(userBaseQuery.getId());
		if (communityIds != null && communityIds.size() == 0) {
			communityIds.add(0);// 防止出错
		}
		CommunityBaseQuery communityBaseQuery = new CommunityBaseQuery();
		communityBaseQuery.setIncludeIds(communityIds);
		communityBaseQuery.setIsShow(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(1, 3, new PageOrder(Direction.ASC, "id"));
		Page<CommunityBaseVo> communityPage = communityBaseService.queryPageList(communityBaseQuery, pageKit);
		ret.put("communityPage", communityPage);// 他、她加入的社区
		if (null != curUserId && !isMySelf) {
			List<Integer> ourCircleIds = circleMemberService.queryOurcircleIds(curUserId, userBaseQuery.getId());
			ret.put("ourCircleIds", ourCircleIds);// 和他共同的圈子id

			List<Integer> ourCommunityIds = communityJoinService.queryOurCommunityIds(curUserId, userBaseQuery.getId());
			ret.put("ourCommunityIds", ourCommunityIds);// 和他共同的社区id
		}

		if (isMySelf) {// 自己的轨迹
			UserMessageQuery userMessageQuery = new UserMessageQuery();
			userMessageQuery.setFromId(curUserId);
			Pageable pageKit3 = new PageKit(userMessageQuery.getPage(), 3, new PageOrder(Direction.DESC, "newAt"));
			List<UserMessageVo> userMessageVos = userMessageService.queryList(userMessageQuery, pageKit3);
			UserBaseVo toUserBaseVo = null;
			for (UserMessageVo userMessageVo : userMessageVos) {
				toUserBaseVo = userBaseService.getById(userMessageVo.getToId());
				userMessageVo.setContent("我对" + toUserBaseVo.getNick() + "敲门说" + userMessageVo.getContent());
			}
			ret.put("userMessageVos", userMessageVos);// 我的敲门轨迹，显示3个
		}
		return ret;
	}

	/**
	 * 分页
	 * 
	 * @param userBaseQuery
	 * @return
	 */
	@RequestMapping(value = "/page")
	@ResponseBody
	public Result page(@RequestBody UserBaseQuery userBaseQuery) {
		Result ret = new Result();
		if (StringUtil.isNotBlank(userBaseQuery.getSearch())) {
			UserIdentityQuery userIdentityQuery = new UserIdentityQuery();
			userIdentityQuery.setType(UserIdentityTypeDictEnum.PHONE.getCode());
			userIdentityQuery.setIdentity(userBaseQuery.getSearch());
			List<UserIdentityVo> userIdentityList = userIdentityService.queryList(userIdentityQuery);
			if (null != userIdentityList && userIdentityList.size() > 0) {
				List<Integer> containIds = new ArrayList<Integer>();
				for (UserIdentityVo userIdentityVo : userIdentityList) {
					containIds.add(userIdentityVo.getUserId());
				}
				userBaseQuery.setSearch(null);
				userBaseQuery.setContainIds(containIds);
			}

		}
		Pageable pageKit = new PageKit(userBaseQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<UserBaseVo> page = userBaseService.queryPageList(userBaseQuery, pageKit);
		ret.put("page", page);
		return ret;
	}

	/**
	 * 用户搜索页面
	 * 
	 * @param userBaseQuery
	 * @return
	 */
	@RequestMapping(value = "/search")
	@ResponseBody
	public Result search(@RequestBody UserBaseQuery userBaseQuery) {
		Result ret = new Result();
		String search = userBaseQuery.getSearch();
		if (StringUtil.isNotBlank(search)) {
			UserIdentityQuery userIdentityQuery = new UserIdentityQuery();
			userIdentityQuery.setType(UserIdentityTypeDictEnum.PHONE.getCode());
			userIdentityQuery.setIdentity(userBaseQuery.getSearch());
			List<UserIdentityVo> userIdentityList = userIdentityService.queryList(userIdentityQuery);
			if (null != userIdentityList && userIdentityList.size() > 0) {
				List<Integer> containIds = new ArrayList<Integer>();
				for (UserIdentityVo userIdentityVo : userIdentityList) {
					containIds.add(userIdentityVo.getUserId());
				}
				userBaseQuery.setSearch(null);
				userBaseQuery.setContainIds(containIds);
			}

		}
		// 搜索用户
		List<UserBaseVo> listUsers = userBaseService.queryList(userBaseQuery);
		// 搜索圈子
		CircleBaseQuery circleBaseQuery = new CircleBaseQuery();
		circleBaseQuery.setStatus(CircleBaseStatusDictEnum.NORMAL.getCode());
		circleBaseQuery.setAuditStatus(CircleAuditStatusEnum.AUDIT_PASS.getCode());// 审核通过的
		List<Integer> includeOpenLevelList = Lists.newArrayList();
		includeOpenLevelList.add(CircleOpenLevelDictEnum.FULL_OPEN.getCode());
		includeOpenLevelList.add(CircleOpenLevelDictEnum.HALF_OPEN.getCode());
		circleBaseQuery.setIncludeOpenLevels(includeOpenLevelList);// 只显示（公开或半开放的圈子）

		List<Integer> includeFunctypeList = Lists.newArrayList();
		includeFunctypeList.add(CircleFuncTypeDictEnum.ALL.getCode());
		includeFunctypeList.add(CircleFuncTypeDictEnum.TOPICONLY.getCode());
		circleBaseQuery.setIncludeFuncTypes(includeFunctypeList);// 去掉群聊

		circleBaseQuery.setSearch(search);
		List<CircleBaseVo> listCircless = circleBaseService.queryList(circleBaseQuery);
		// 搜索社区
		CommunityBaseQuery communityBaseQuery = new CommunityBaseQuery();
		communityBaseQuery.setSearch(search);
		communityBaseQuery.setIsShow(YesNoEnum.YES.getCode());
		List<CommunityBaseVo> listCommunitys = communityBaseService.queryList(communityBaseQuery);

		ret.put("listUsers", listUsers);
		ret.put("listCircless", listCircless);
		ret.put("listCommunitys", listCommunitys);
		return ret;
	}

	/**
	 * 我的业务数量
	 * 
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/myBizzInfo")
	@ResponseBody
	public Result myBizzInfo() {
		Result ret = new Result();
		Integer curUserId = super.getUserId();
		CircleMemberQuery query = new CircleMemberQuery();
		query.setMemberId(curUserId);
		int myCircleCount = circleBaseService.queryMyCircles(query).size();// 江湖【我的圈子数量】
		int myTopicCount = circleTopicService.queryMyTopicCount(curUserId);// 内容【话题】

		CommunityJoinQuery communityJoinQuery = new CommunityJoinQuery();
		communityJoinQuery.setUserId(curUserId);
		communityJoinQuery.setStatus(CommunityJoinStatusDictEnum.NORMAL.getCode());
		int myCommunityCount = communityJoinService.queryCount(communityJoinQuery);// 本地【社区】

		// 服务数量
		// int myServiceCount =
		// homemakingBaseService.myHomemakingBaseCount(curUserId);
		OrderBaseQuery sellerOrder = new OrderBaseQuery();
		sellerOrder.setSellerId(curUserId);
		sellerOrder.setOrderStatus(OrderBaseOrderStatusDictEnum.UNCOMPLETED.getCode());// 未完成
		sellerOrder.setPayStatus(OrderBasePayStatusDictEnum.PAYED.getCode());// 已付款
		sellerOrder.setDeliveryStatus(OrderBaseDeliveryStatusDictEnum.UNSEND.getCode());// 未发货
		int waitSendCount = orderBaseService.queryCount(sellerOrder);
		OrderBaseQuery buyerOrder = new OrderBaseQuery();
		buyerOrder.setBuyerId(curUserId);
		buyerOrder.setOrderStatus(OrderBaseOrderStatusDictEnum.UNCOMPLETED.getCode());// 未完成
		buyerOrder.setPayStatus(OrderBasePayStatusDictEnum.PAYED.getCode());// 已付款
		buyerOrder.setDeliveryStatus(OrderBaseDeliveryStatusDictEnum.SENDED.getCode());// 发货
		int waitReceiptCount = orderBaseService.queryCount(buyerOrder);
		DeliveryOrderQuery deliveryOrder = new DeliveryOrderQuery();
		deliveryOrder.setCreatorId(curUserId);
		deliveryOrder.setStatus(DeliveryOrderStatusDictEnum.NoSign.getCode());
		int deliveryOrderCount = deliveryOrderService.queryCount(deliveryOrder);
		int myFriendCount = userFriendService.queryFriendCount(curUserId);
		int myActivityCount = activitySignService.getCountByUserId(curUserId);
		ret.put("myCircleCount", myCircleCount);
		ret.put("myTopicCount", myTopicCount);
		ret.put("myCommunityCount", myCommunityCount);
		ret.put("myServiceCount", waitSendCount + waitReceiptCount + deliveryOrderCount);
		ret.put("myFriendCount", myFriendCount);//圈友数量
		ret.put("myActivityCount", myActivityCount);
		return ret;
	}

	/**
	 * 查找用户-添加好友 分页显示
	 * 
	 * @param userBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/search4AddFriend")
	@ResponseBody
	public Result search4AddFriend(@RequestBody UserBaseQuery userBaseQuery) {
		Result ret = new Result();
		Integer curUserId = super.getUserId();// 我的id
		Pageable pageKit = new PageKit(userBaseQuery.getPage(), new PageOrder(Direction.ASC, "nick"));
		Page<UserBaseVo> userPage = userBaseService.queryPageList(userBaseQuery, pageKit);
		List<UserBaseVo> userList = userPage.getContent();
		if (userList != null && userList.size() > 0) {
			for (UserBaseVo user : userList) {
				if (userFriendService.isFriend(curUserId, user.getId())) {
					user.setFriendShipDesc("已添加");
					user.setShowAddBtn(false);
				} else if (userFriendApplyService.isFriendApplyExist(curUserId, user.getId())) {
					user.setFriendShipDesc("等待验证");
					user.setShowAddBtn(false);
				} else {
					user.setFriendShipDesc("");
					user.setShowAddBtn(true);
				}
				if (curUserId.equals(user.getId())) {
					// 自己
					user.setFriendShipDesc("自己");
					user.setShowAddBtn(false);
				}
			}
		}
		ret.put("userPage", userPage);
		return ret;
	}

	/**
	 * 修正用户中文全拼
	 * 
	 * @return
	 */
	@RequestMapping(value = "/fixUserChnsPell")
	@ResponseBody
	public Result fixUserChnsPell() {
		Result result = new Result();
		int updateCount = 0;
		UserBase user4update = null;
		UserBaseQuery userQ = new UserBaseQuery();
		int userCount = userBaseService.queryCount(userQ);
		int pageCount = (userCount / 30) + 1;// 总页数
		for (int i = 1; i <= pageCount; i++) {
			Pageable pageKit = new PageKit(i, 30, new PageOrder(Direction.ASC, "id"));
			List<UserBaseVo> userList = userBaseService.queryList(userQ, pageKit);
			if (userList != null && userList.size() > 0) {
				for (UserBaseVo user : userList) {
					if (StringUtil.isNotBlank(user.getNick()) && StringUtil.isBlank(user.getChnSpell())) {
						String pinYin4Nick = PinyingUtil.hanziToPinyinNoSpace(user.getNick()).toUpperCase();
						if (StringUtil.isNotBlank(pinYin4Nick)) {
							user4update = new UserBase();
							user4update.setId(user.getId());
							user4update.setChnSpell(pinYin4Nick);// 全拼大写
							int updateC = userBaseService.updateById(user4update);
							if (updateC > 0) {
								updateCount++;
							}
						}
					}
				}
			}
		}
		result.put("fixMsg", "本次更新用户中文全拼：" + updateCount + "个");
		return result;
	}

	/**
	 * 访问其他用户的空间资料V1
	 * 
	 * @param userBaseQuery
	 * @return
	 */
	@RequestMapping(value = "/personalV1")
	@ResponseBody
	public Result personalV1(@RequestBody UserBaseQuery userBaseQuery) {
		Result ret = new Result();
		Integer curUserId = super.getUserId();
		UserBaseVo userBaseVo = userBaseService.getById(userBaseQuery.getId());
		if (userBaseVo == null) {
			ret.addErr("找不到这个用户!");
			return ret;
		}
		ret.put("userBaseVo", userBaseVo);
		boolean isMySelf = false;
		boolean isBlack = false;
		boolean isFriend = false;
		if (curUserId == userBaseQuery.getId()) {// 访问自己
			isMySelf = true;
		}
		isBlack = userBlackService.isBlack(curUserId, userBaseQuery.getId());// 是否黑名单
		isFriend = userFriendService.isFriend(curUserId, userBaseQuery.getId());// 是否是好友

		ret.put("isBlack", isBlack);
		ret.put("isMySelf", isMySelf);
		ret.put("isFriend", isFriend);

		// 用户的动态
		CircleTopicQuery circleTopicQuery = new CircleTopicQuery();
		circleTopicQuery.setIsShow(YesNoEnum.YES.getCode());
		circleTopicQuery.setCreatorId(userBaseQuery.getId());// 版主

		Pageable pageKit2 = new PageKit(circleTopicQuery.getPage(), 1, new PageOrder(Direction.DESC, "newAt"));
		Page<CircleTopicVo> circleTopicPage = circleTopicService.queryPageList(circleTopicQuery, pageKit2);
		UserBaseVo userVo = null;
		for (CircleTopicVo topicVo : circleTopicPage) {
			userVo = userBaseService.getById(topicVo.getCreatorId());
			topicVo.setCreatorImg(userVo.getLogo());
			topicVo.setCreatorName(userVo.getNick());
			topicVo.setImgString(topicVo.getImg(), SysConst.IMG_POSTFIX);
		}
		// 他、她加入的圈子
		List<Integer> herCircleIds = circleMemberService.queryMyOpenCircleIds(userBaseQuery.getId());
		List<CircleBaseVo> circleList = circleBaseService.queryListByCircleIds(herCircleIds);
		ret.put("circleList", circleList);// 他、她加入的圈子

		// 他、她加入的社区
		List<Integer> communityIds = communityJoinService.queryMyCommuntiyIds(userBaseQuery.getId());
		if (communityIds != null && communityIds.size() == 0) {
			communityIds.add(0);// 防止出错
		}
		CommunityBaseQuery communityBaseQuery = new CommunityBaseQuery();
		communityBaseQuery.setIncludeIds(communityIds);
		communityBaseQuery.setIsShow(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(1, 3, new PageOrder(Direction.ASC, "id"));
		List<CommunityBaseVo> communityList = communityBaseService.queryList(communityBaseQuery, pageKit);
		ret.put("communityList", communityList);// 他、她加入的社区
		if (null != curUserId && !isMySelf) {
			List<Integer> ourCircleIds = circleMemberService.queryOurcircleIds(curUserId, userBaseQuery.getId());
			ret.put("ourCircleIds", ourCircleIds);// 和他共同的圈子id

			List<Integer> ourCommunityIds = communityJoinService.queryOurCommunityIds(curUserId, userBaseQuery.getId());
			ret.put("ourCommunityIds", ourCommunityIds);// 和他共同的社区id
		}
		UserValueVo userValueVo = userValueService.getById(userBaseQuery.getId());
		ret.put("userValueVo", userValueVo);
		return ret;
	}
	
	/**
	 * 获取用户的职业
	 * @return
	 */
	
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/getProfession")
	@ResponseBody
	public Result getProfession() {
		ArrayList<String> professionlist = new ArrayList<String>();
        professionlist.add("农、林、牧、渔业");
		professionlist.add("采矿业");
		professionlist.add("制造业");
		professionlist.add("电力、热力、燃气及水生产和供应业");
		professionlist.add("建筑业");
		professionlist.add("批发和零售业");
		professionlist.add("交通运输、仓储和邮政业");
		professionlist.add("住宿和餐饮业");
		professionlist.add("信息传输、软件和信息技术服务业");
		professionlist.add("金融业");
		professionlist.add("房地产业");
		professionlist.add("租赁和商务服务业");
		professionlist.add("科学研究和技术服务业");
		professionlist.add("水利、环境和公共设施管理业");
		professionlist.add("居民服务、修理和其他服务业");
		professionlist.add("教育");
		professionlist.add("公共管理、社会保障和社会组织");
		professionlist.add("文化、体育和娱乐业");
		professionlist.add("国际组织");
		Result result = new Result(RespEnum.OK,professionlist);
		return result;
	}

	/**
	 * 获取用户的职业
	 * @return
	 */
	@RequestMapping(value = "/getUserBaseConfig")
	@ResponseBody
	public Result getUserBaseConfig(@RequestBody UserBaseConfigQuery userBaseConfigQuery) {
		List<UserBaseConfig> professionList = new ArrayList<UserBaseConfig>();
		if(userBaseConfigQuery.getType() == 1){ //职业
			professionList = userBaseService.queryUserConfigProfession();
		}if(userBaseConfigQuery.getType()  == 2){//爱好
			professionList = userBaseService.queryUserConfigHobby();
		} if(userBaseConfigQuery.getType()  == 3){//美食
			professionList = userBaseService.queryUserConfigDelicacy();
		} if(userBaseConfigQuery.getType()  == 4){//家乡
			professionList = userBaseService.queryUserConfigHometown();
		} if(userBaseConfigQuery.getType()  == 5){//娱乐
			professionList = userBaseService.queryUserConfigEntertainment();
		}
		Result result = new Result();
        result.put("data",professionList);
		return result;
	}

}
